package com.windfone.web.action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.windfone.model.Product;
import com.windfone.model.ProductType;
import com.windfone.service.ProductService;
import com.windfone.service.ProductTypeService;

public class SearchCenterAction extends ActionSupport {
	private ProductService productService;
	private ProductTypeService productTypeService;
	private int typeId =-1;
	private int status ;
	private List<Product> products;
	private String beginPrice;
	private String endPrice;
	private String productName;
	private List<ProductType> productTypeList;
	public List<Product> getProducts() {
		return products;
	}

	public ProductTypeService getProductTypeService() {
		return productTypeService;
	}

	public String getBeginPrice() {
		return beginPrice;
	}

	public String getEndPrice() {
		return endPrice;
	}

	public String getProductName() {
		return productName;
	}

	public void setBeginPrice(String beginPrice) {
		this.beginPrice = beginPrice;
	}

	public void setEndPrice(String endPrice) {
		this.endPrice = endPrice;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public List<ProductType> getProductTypeList() {
		return productTypeList;
	}

	public void setProductTypeService(ProductTypeService productTypeService) {
		this.productTypeService = productTypeService;
	}

	public void setProductTypeList(List<ProductType> productTypeList) {
		this.productTypeList = productTypeList;
	}

	public void setProducts(List<Product> products) {
		this.products = products;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	@Override
//	SELECT p.* FROM product p,producttype t 
//	WHERE  p.productTypeId=t.id AND p.status=? AND p.productTypeId=? 
//	ORDER BY  p.pointSeq DESC,p.id
	public String execute() throws Exception {
		productTypeList = productTypeService.listProductType();
		String whereStr = "  p.productTypeId=t.id AND p.status=1  ";
		boolean flash = false;
		if ( typeId !=-1){
			whereStr=whereStr+" AND p.productTypeId="+typeId;
			flash =true;
		}
		if ( productName!=null && !productName.equals("") && productName.indexOf("*") ==-1 &&  productName.indexOf("%") ==-1){
			whereStr=whereStr+" AND p.name LIKE '%"+productName+"%'";
			flash =true;
		}
		if ( beginPrice!=null && !beginPrice.equals("") && isInteger( beginPrice )){
			whereStr=whereStr+" AND p.price >="+beginPrice;
			flash =true;
		}
		if ( endPrice!=null && !endPrice.equals("") && isInteger( endPrice )){
			whereStr=whereStr+" AND p.price <="+endPrice;
			flash =true;
		}
		if ( flash )
			products = productService.getAllProductByWherecondition(whereStr );
		else 
			products = new ArrayList<Product>();
		productName = "";
		beginPrice = "";
		endPrice = "";
		typeId=-1;
		return SUCCESS;
	}

	public int getTypeId() {
		return typeId;
	}

	public void setTypeId(int typeId) {
		this.typeId = typeId;
	}
	
	private  boolean isInteger(String value) {
		try {
			new Integer(value);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}
}
